//
//  HRDynamicInfoViewModel.m
//  Hear
//
//  Created by mac on 2017/4/11.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRDynamicInfoViewModel.h"
#import "HRDynamicCommentModel.h"
#import "HRDynamicModel.h"
#import "HRFollowDynamicLayout.h"

@implementation HRDynamicInfoViewModel



-(void)postDynamicCommentParam:(HRDynamicCommentRequestModel*)requstModel AndCommentModel:(void(^)(HRDynamicCommentModel *commentModel))commentModel{
    YTKApi *api = [YTKApi apiPostWithUrl:@"MomentsInfo/Post_SaveMomentsComment" parameter:requstModel];
    [api getSuccessToken:^() {
        api.foceNoTocken = NO;
        [api setRequestSerializerType:YTKRequestSerializerTypeHTTP];
        [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
            
            NSDictionary *dict = request.responseJSONObject;
            
            HRDynamicCommentModel *model = [HRDynamicCommentModel modelWithJSON:dict[@"BodyContent"]];
            
            commentModel(model);
        } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
            commentModel(nil);
        }];
    }];
}

-(void)getCommentListParam:(HRDynamicGetCommentRequestModel*)requstModel isSeccess:(void(^)(NSArray *commentArr))isSeccess{
    YTKApi *api = [YTKApi apiGetWithUrl:@"MomentsInfo/Get_GetComments" parameter:requstModel];
    api.cacheTime = 0;
    [api getSuccessToken:^() {
        api.foceNoTocken = NO;
        [api setRequestSerializerType:YTKRequestSerializerTypeJSON];
        [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
            
            NSDictionary *dict = request.responseJSONObject;
            
            NSArray *resultArr = [NSArray modelArrayWithClass:[HRDynamicCommentModel class] json:dict[@"BodyContent"]];
            for (HRDynamicCommentModel *model in resultArr) {
                model.ChildComments = [NSArray modelArrayWithClass:[HRDynamicCommentModel class] json:model.ChildComments];
            }
            
            
            isSeccess(resultArr);
        } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
            isSeccess(NO);
        }];
    }];
}

-(void)deleteDynamic:(NSDictionary *)requstDict isSeccess:(void(^)(BOOL isSeccess))isSeccess{
    YTKApi *api = [YTKApi apiDeleteWithUrl:@"MomentsInfo/Delete_DeleteMoments" para:requstDict];
    [api getSuccessToken:^() {
        api.foceNoTocken = NO;
        [api setRequestSerializerType:YTKRequestSerializerTypeJSON];
        [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
            
            NSDictionary *dict = request.responseJSONObject;
            
            
            
            
            isSeccess(YES);
        } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
            isSeccess(NO);
        }];
    }];
}


-(void)deleteComment:(NSDictionary *)requstDict isSeccess:(void(^)(BOOL isSeccess))isSeccess{
    YTKApi *api = [YTKApi apiDeleteWithUrl:@"MomentsInfo/Delete_DeleteComment" para:requstDict];
    [api getSuccessToken:^() {
        api.foceNoTocken = NO;
        [api setRequestSerializerType:YTKRequestSerializerTypeJSON];
        [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
            
            NSDictionary *dict = request.responseJSONObject;
            
            
            
            
            isSeccess(YES);
        } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
            isSeccess(NO);
        }];
    }];
}


-(void)getDynamicInfoContent:(NSDictionary*)requstDict isSeccess:(void(^)(HRFollowDynamicLayout *layout))isSeccess{
    YTKApi *api = [YTKApi apiGetWithUrl:@"MomentsInfo/Get_GetSingleMoment" para:requstDict];
    [api getSuccessToken:^() {
        api.foceNoTocken = NO;
        [api setRequestSerializerType:YTKRequestSerializerTypeJSON];
        [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
            
            NSDictionary *dict = request.responseJSONObject;
            
            HRFollowDynamicLayout *layout = [HRFollowDynamicLayout new];
            layout.style = HRFollowLayoutStyleDetail;
            HRDynamicModel *dynamicModel = [HRDynamicModel modelWithJSON:dict[@"BodyContent"]];
            if([dynamicModel.MomentType intValue] == 2){
                dynamicModel.pics = [dynamicModel.MomentDetai jsonValueDecoded];
            }else if ([dynamicModel.MomentType intValue] == 5){
                dynamicModel.soundAndImageConentModel = [HRDynamicModel modelWithJSON:dynamicModel.MomentDetai];
                dynamicModel.soundAndImageConentModel.pics = [dynamicModel.soundAndImageConentModel.PhotoUrl jsonValueDecoded];
            }
            
            
            
            dynamicModel.comentListArray = [NSArray modelArrayWithClass:[HRDynamicCommentModel class] json:dynamicModel.commentList];
            layout.dynamicModel = dynamicModel;
            
            isSeccess(layout);
        } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
            isSeccess(nil);
        }];
    }];
}




@end
